import rdflib
import json
from tqdm import tqdm
import os
curpath = os.path.dirname(os.path.abspath(__file__))

g = rdflib.Graph()
data = g.parse(location=curpath + "/musicknowledge.ttl", format="turtle")

res = {}
for d in tqdm(data):
    s = str(d[0]).split('#')[1]
    p = str(d[1]).split('#')[1]
    o = str(d[2])
    if '#' in o:
        o = o.split('#')[1]

    if s not in res:
        res[s] = {}
    if p not in res[s]:
        res[s][p] = []
    res[s][p].append(o)

json.dump(res, open(curpath + '/data.json', 'w'))
